<?php
namespace Wap\Controller;
use Think\Controller;
class IndexController extends Controller{
    
    public function index(){
        $this->display();
    }
    
    /**
     * 云麻豆首页
     * @param type $id         用户id
     * @param type $p           页码
     * @param type $per_page    单页条目数
     */
    public function home($id=0, $p=1, $per_page=12, $callback=''){
        //获取单页数据
        $page = page('User_model', $per_page, array('status' => 1, 'model_card' => array('NEQ','')), 
                array('model_card_updatetime' => 'desc'), '', 'uid, nickname, model_card, favour');
        $model = $page['list']; //单页数据
        if($model != NULL){
            foreach ($model as $key => $value){
                $model[$key]['model_card'] = $this->www_url . $value['model_card'];
                $model[$key]['is_favour'] = 0;          //用户是否赞过该微博
                if($id!=0 && M('Favour')->where(array('uid' => $id, 'fid' => $value['uid'], 'type' => 1))->find()){
                    $model[$key]['is_favour'] = 1;
                }
            }
            $data = array(
                "code" => 1, 
                "msg" => "首页信息获取成功", 
                "data" => $model, 
                "total_num" => $page['total'],
                'p'     => $p,
            );
        } else {
            $data = array(
                "code" => 0, 
                "msg" => "首页暂无信息",
            );
        }
//        $this->ajaxReturn($data);
        jsonP($data, $callback);
    }
    
    /**
     * 麻豆卡展示
     * @param type $id  模特id
     * @param type $uid 登陆用户id
     */
    public function getModelShow($id=0, $uid=0, $callback=''){
        $model = M('User_model')->field('uid,model_card,favour')->where(array('uid' => $id))->find();
            if($model){
            $model['is_favour'] = 0;          //用户是否赞过该模特
            if($id!=0 && M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => 0))->find()){
                $model['is_favour'] = 1;
            }
            $data = array(
                'code'  => 1,
                'msg'   => '模特展示获取成功',
                'data'  => $model,
            );
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '模特信息获取失败',
            );
        }
        jsonP($data, $callback);
    }

    /**
     * 获取模特相册和模特详情的公共部分
     * @param int $id  模特id
     * @param int $uid 用户id
     */
    public function getPublicModelPart($id=0, $uid=0, $callback=''){
        $model = M('User')->field('avatar_url, nickname')->where(array('id' => $id))->find();
        if($model != NULL){
            $model['picnum'] = M('Weibo')->where(array('uid' => $id))->join('md_weibo_pic on md_weibo.id=md_weibo_pic.w_id')->count();
            $model['fansnum'] = M('Follow')->where(array('pid' => $id))->count();
            $model['satisfynum'] = M('Apply_portrait')->where(array('mid' => $id, 'is_satisfy' => 1))->count();
            $tmp = M('User_model')->field('value')->where(array('uid' => $id))->find();
            $model['value'] = $tmp['value'];
            $data = array(
                'code'  => 1,
                'msg'   => '获取成功',
                'data'  => $model,
            );
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '获取失败',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 获取模特朋友圈状态列表
     * @param int $id 模特id
     * @param int uid 用户id
     * @param int p 页码
     * @param int per_page 单页条目数
     * @return 朋友圈状态id，发布日期，发布图片封面图，文字内容
     */
    public function getWeiboList($uid=0, $id=0, $p=1, $per_page=4, $callback=''){
        //获取单页微博列表以及总的条目数
        $model = page("Weibo", $per_page, array('uid' => $id, 'status' => 1), array('add_time'=>'desc'));
        $weibo = $model['list'];  //单页微博列表 
        if($weibo != NULL){
            foreach ($weibo as $w => $v){
                unset($weibo[$w]['add_time']);
                $weibo[$w]['day'] = date('d', $v['add_time']);
                $weibo[$w]['month'] = date('m', $v['add_time']);             
                //获取微博照片
                $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $v['id']))->select();
                if($pic != NULL){
                    foreach ($pic as $k => $p){
                        $pic[$k] = $this->www_url . $p['pic_url'];
                    }
                }
                $weibo[$w]['pic'] = $pic;
            }
            $data = array(
                'code'  => 1,
                'msg'   => '微博列表获取成功',
                'data'  => $weibo,
                'count' => $model['total'],
            );
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '暂无微博',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
    * 查看微博详情
    * @param int $id  微博id
    * @param int $uid 用户id
    * @return json code、msg、data：微博内容、照片、赞数目、用户是否赞过
    */
    public function getWeiboDetail($id=0, $uid=0, $callback=''){

        $detail = M('Weibo')->field('content, favour')->where(array('status' => 1, 'id' => $id))->find();      
        if($detail != NULL){
            $detail['is_favour'] = 0; //用户是否赞过微博，0未赞
            if($uid!=0 && M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => 1))->find()){
                $detail['is_favour'] = 1;
            }
            $detail['comment_num'] = M('Comment')->where(array('weibo_id' => $id, 'type' => 0))->count();
            $detail['favour_num']  = M('Favour')->where(array('fid' => $id, 'type' => 1))->count();
            //获取微博照片
            $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $id))->select();
            foreach ($pic as $k => $v){
                $pic[$k] = $this->www_url . $v['pic_url'];
            }
            $detail['pic'] = $pic;
            $data = array(
                'code'  => 1,
                'msg'   => '模特微博详情获取成功',
                'data'  => $detail,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '模特微博详情获取失败',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 查看模特详情
     * @param int $id  模特id
     * @param int $uid 用户id
     * @return json code、msg、data：。。。
     */
    public function getModelDetail($id=0, $uid=0, $callback=''){

        $fields = 'city,age,height,weight,bust,waistline,hipline,shoe_size,job,style';
        $basic_info = M('User_model')->field($fields)->where(array('uid' => $id))->find();
        if($basic_info != NULL){
            $city = $this->getCity($basic_info['city']);            
            $basic_info['city'] = $city['city']['name'];
            $parameter = array('style', 'job');
            $config = get_array_configs($parameter);    //从配置中获取style和job的值
            if($basic_info['style'] != -1){
                $basic_info['style'] = array('code' => $basic_info['style'], 'msg' => $config['style'][$basic_info['style']]);
            }else{
                $basic_info['style'] = array('code' => 0, 'msg' => $config['style'][0]);
            }
            if($basic_info['job'] != -1){
                $basic_info['job']  = array('code' => $basic_info['job'], 'msg' => $config['job'][$basic_info['job']]);
            }else{
                $basic_info['job']  = array('code' => 0, 'msg' => $config['job'][0]);
            }
            $basic_info['is_attention'] = 0;
            if(M('Follow')->where(array('uid' => $uid, 'pid' => $id))->find()){
                $basic_info['is_attention'] = 1;
            }            
            $data = array(
                'code' => 1,
                'msg'  => '模特详情信息获取成功!',
                'data' => $basic_info,
            );
        }else{
            $data = array(
                'code' => 0, 
                'msg'=>'模特详情获取失败！'
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 获取城市信息
     * @param type 地区id
     * @return type
     */
    public function getCity($districtid){
        $district = M('City')->where(array('id' => $districtid))->find();
        $city = M('City')->where(array('id' => $district['pid']))->find();
        $provience = M('City')->where(array('id' => $city['pid']))->find();
        $data = array(
            'provience' => array('id' => $provience['id'], 'name' => $provience['name']),
            'city' => array('id' => $city['id'], 'name' => $city['name']),
            'district'  => array('id' => $district['id'], 'name' => $district['name']),
        );
        return $data;
    }
    
    /**
    * 关注
    * @param type $uid 关注者（登录用户）id
    * @param type $pid 被关注者id
    * @return json code/msg
    */
    public function attention($uid=0, $pid=0, $callback=''){
        if($uid!=0 && $pid!=0){
            $relation = 1;
            //查看用户是否已关注过
            $attented = M('Follow')->where(array('uid' => $uid, 'pid' => $pid))->find();
            if($attented == NULL){
                //查看对方是否关注过我
                if(M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->find()){
                    $relation = 3;
                }
                $data = array(
                    'uid'       => $uid,
                    'pid'       => $pid,
                    'relation'  => $relation,
                );
                if(M('Follow')->add($data)){
                    $tmp = array(
                        'code'  => 1,
                        'msg'   => '成功',
                    );
                } else {
                    $tmp = array(
                        'code' => -1,
                        'msg'  => '失败',
                    );
                }
            }else{
                $tmp = array(
                    'code' => 0,
                    'msg'  => '已关注'
                );
            }
            jsonP($tmp, $callback);
        }
    }
    
    /**
     * 取消关注
     * @param int 用户id
     * @param int 被取消关注者id
     */
    public function cancelAttention($uid=0, $pid=0, $callback=''){

        //查看对方是否关注过我
        if(M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->find()){
            M('Follow')->where(array('uid' => $pid, 'pid' => $uid))->save(array('relation' => 1)); //关注过，将互粉改为单向关注
        }
        $data = array(
            'uid'       => $uid,
            'pid'       => $pid,
        );
        if(M('Follow')->where($data)->delete()){
            $tmp = array(
                'code'  => 1,
                'msg'   => '成功',
            );
        } else {
            $tmp = array(
                'code'  => 0,
                'msg'  => '失败',
            );
        }
        jsonP($tmp, $callback);
    }
    
    /**
     * 获取评论页面微博图片及个人信息
     * @param type $uid 用户id
     * @param type $id  微博id
     * @return code、msg、data：。。
     */
    public function getComments($uid=0, $id=0, $callback=''){

        $comment = M('Weibo')->where(array('md_weibo.status' => 1, 'md_user.status'=> 1, 'md_weibo.id' => $id))
                ->field(array('md_user.id' => 'uid', 'content', 'favour'))
                ->join('md_user on md_user.id = md_weibo.uid')
                ->find();
        if($comment != NULL){
            //获取该条微博的所有图片
            $pic = M('Weibo_pic')->field('pic_url')->where(array('w_id' => $id))->select();
            foreach ($pic as $key => $value){
                $pic[$key]  = $this->www_url . $value['pic_url'];
            }
            $comment['pic'] = $pic;
            //处理微博添加时间
            
            $is_favour = 0; //是否赞过，0未赞
            if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => 1))->find()){
                $is_favour = 1;
            }
            $comment['is_favour'] = $is_favour;
            $data = array(
                'code'  => 1,
                'msg'   => '微博发布人信息获取成功',
                'data'  => $comment,
            );
        } else {
            $data = array(
                'code'  => 0,
                'msg'   => '微博发布人信息获取失败',                
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 获取评论列表
     * @param int $id       微博id
     * @param int $p        页码
     * @param int $per_page 单页条目数
     * @param int $type 0微博评论，1试衣间评论
     * @return json 评论人nickname、头像、评论时间、内容
     */
    public function getCommontsList($id=0, $p=1, $per_page=6, $type=0, $callback=''){
        
        $page = page('Comment', $per_page, array('weibo_id' => $id, 'md_comment.status' => 1, 'md_user.status' => 1, 'type' => $type), 
                array('add_time' => 'desc'), 'md_user on md_comment.uid = md_user.id', 'md_comment.id, nickname, avatar_url, content, add_time');
        $comments = $page['list'];
        
        if($comments != NULL){
            foreach ($comments as $k => $v){
                $comments[$k]['add_time'] = date('m-d H:i', $v['add_time']);
                $comments[$k]['avatar_url'] = $this->www_url . $v['avatar_url'];
            }
            $data = array(
                'code'  => 1,
                'msg'   => '评论获取成功',
                'data'  => $comments,
                'count' => $page['total'], //总的评论数
            );
        }  else {
            $data = array(
                'code'  => 0,
                'msg'   => '暂无评论',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 评论
     * @param int $wid       微博id
     * @param int $uid        评论人id
     * @param string $content 评论内容
     * @param int $type 0评论微博，1评论试衣间状态
     */
    public function comment($wid=0, $uid=0, $content='', $type=0, $callback=''){
        
        $data = array(
            'uid'       => $uid,
            'weibo_id'  => $wid,
            'content'   => $content,
            'add_time'  => NOW_TIME,
            'type'      => $type,
        );
        if(M('Comment')->add($data)){
            $data = array(
                'code'      => 1,
                'msg'       => '评论成功',
            );            
        }else{
            $data = array(
                'code'  => 0,
                'msg'   => '评论失败，请重试',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 赞
     * @param type $type 赞类型 0模特，1微博，2试衣间
     * @param type $uid  赞用户id
     * @param type $id   被赞模特/微博/试衣间id
     */
    public function favour($type=0, $uid=0, $id=0, $callback=''){

        if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => $type))->find()){
            $data = array(
                'code' => 0,
                'msg'  => '已赞',
            );
        }else{
            M('Favour')->add(array('uid' => $uid, 'fid' => $id, 'type' => $type, 'createtime' => NOW_TIME));
            if($type == 0){
                M('User_model')->where(array('uid' => $id))->setInc('favour');
            }elseif($type == 1){
                M('Weibo')->where(array('id' => $id))->setInc('favour');
            }else{
                M('Invite_experience')->where(array('id' => $id))->setInc('favour');
            }
            $data = array(
                'code' => 1,
                'msg'  => '赞成功',
            );
        }
        jsonP($data, $callback);
    }
    
    /**
     * 取消赞
     * @param int $type 类型：0模特，1微博，2试衣间
     * @param int $uid  取消赞用户id
     * @param int $id   被取消赞用户id
     */
    public function cancelFavour($type=0, $uid=0, $id=0, $callback=''){

        if(M('Favour')->where(array('uid' => $uid, 'fid' => $id, 'type' => $type))->delete()){
            if($type == 0){
                M('User_model')->where(array('uid' => $id))->setDec('favour');
            }elseif($type = 1){
                M('Weibo')->where(array('id' => $id))->setDec('favour');
            }else{
                M('Invite_experience')->where(array('id' => $id))->setDec('favour');
            }
            $data = array(
                'code' => 1,
                'msg'  => '取消成功',
            );
            
        }else{
            $data = array(
                'code' => 0,
                'msg'  => '失败，不存在记录',
            );
        }
        jsonP($data, $callback);
    }
}